package com.example.testcamara;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import java.io.File;

/**
 * 录制视频，https://bailongma.yuque.com/czya5g/ma84ns/pxfga4osvfg9d1cx
 *
 * @author wupuquan
 */
@SuppressLint("ViewConstructor")
public class Ajx3VideoRecordView extends FrameLayout implements IPageLifeCircleView {

    private final Context mContext;
    private VideoRecordCameraController mCameraController;
    private VideoRecordCameraController.OnPhoneOrientationChangeListener mPhoneOrientationChangeListener;

    public Ajx3VideoRecordView(Context context) {
        super(context);
        mContext = context;
        initView();
    }

    private void initView() {
        setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        VideoRecordTextureView mVideoRecordView = new VideoRecordTextureView(mContext);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        mVideoRecordView.setLayoutParams(params);
        addView(mVideoRecordView);
        mCameraController = new VideoRecordCameraController(mContext, mVideoRecordView);
        mCameraController.setOnPhoneOrientationChangeListener(new VideoRecordCameraController.OnPhoneOrientationChangeListener() {
            @Override
            public void onPhoneOrientationChanged(@VideoRecordCameraController.PhoneOrientation int orientation) {
                onOrientationRotate(orientation);
            }
        });
        mCameraController.setVideoRecordListener(new OnVideoRecordListener() {
            @Override
            public void onStart() {
                notifyRecordStart();
            }

            @Override
            public void onComplete(String filePath) {
                notifyRecordComplete(filePath);
            }
        });
        mCameraController.initCamera();
    }

    void startRecord() {
        mCameraController.startRecordingVideo();
    }

    void stopRecord() {
        mCameraController.stopRecordingVideo();
    }

    /**
     * 切换前置后置摄像头
     */
    void switchCamera() {
        mCameraController.switchCamera();
    }

    /**
     * 切换手电筒开关
     *
     * @param enable true打开手电筒，false关闭
     */
    void toggleFlashlight(boolean enable) {
        mCameraController.toggleFlashlight(enable);
    }

    /**
     * 开始录制时通知AJX
     */
    private void notifyRecordStart() {
    }

    /**
     * 停止录屏保存完成后通知AJX
     */
    private void notifyRecordComplete(String filePath) {
        // code: 返回码，0正常，非0异常
        int code = -1;
        if (filePath != null) {
            File file = new File(filePath);
            if (file.exists() && file.length() > 0) {
                code = 0;
            }
        }

    }

    /**
     * 屏幕旋转
     */
    private void onOrientationRotate(@VideoRecordCameraController.PhoneOrientation int orientation) {
        // 根据设备方向进行相应的处理
        switch (orientation) {
            case VideoRecordCameraController.PHONE_HOME_BUTTON_BOTTOM:
                // 竖屏
                notifyOrientationRotate("bottom");
                break;
            case VideoRecordCameraController.PHONE_HOME_BUTTON_TOP:
                // 反向竖屏
                notifyOrientationRotate("top");
                break;
            case VideoRecordCameraController.PHONE_HOME_BUTTON_LEFT:
                // 左横屏
                notifyOrientationRotate("left");
                break;
            case VideoRecordCameraController.PHONE_HOME_BUTTON_RIGHT:
                // 右横屏
                notifyOrientationRotate("right");
                break;
            default:
                break;
        }
    }

    private String mCurrentOrientation;

    /**
     * 屏幕旋转通知AJX
     */
    private void notifyOrientationRotate( String orientation) {
        if (TextUtils.equals(mCurrentOrientation, orientation)) {
            return;
        }
        mCurrentOrientation = orientation;

    }

    @Override
    public void onPageResume(boolean appSwitch) {

    }

    @Override
    public void onPageStop(boolean appSwitch) {
        mCameraController.onPause();
    }

    @Override
    public void onPageDestroy() {
        mCameraController.release();
        mCameraController = null;
    }

    @Override
    public void onNewIntent() {

    }

}

